package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.bean.AnswerBean;
import com.yuejian_06.yuejian_06.bean.CoachBean;
import com.yuejian_06.yuejian_06.bean.CoachVideoBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.fragment.Constants_l;
import com.yuejian_06.yuejian_06.service.CoachService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by __ZL__ on 2017/9/25.
 */

public class CoachModel {

    private Retrofit retrofit;
    public CoachModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //俱乐部列表
    public void coachList( final LoginListener loginListener){
        CoachService coachService = retrofit.create(CoachService.class);
        Call<List<CoachBean>> call=coachService.coachList();
        call.enqueue(new Callback<List<CoachBean>>() {
            @Override
            public void onResponse(Call<List<CoachBean>> call, Response<List<CoachBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<CoachBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }
    //大师视频列表
    public void getcoachV( String coach_id,final LoginListener loginListener){
        CoachService coachService = retrofit.create(CoachService.class);
        Call<List<CoachVideoBean>> call=coachService.getcoachV(coach_id);
        call.enqueue(new Callback<List<CoachVideoBean>>() {
            @Override
            public void onResponse(Call<List<CoachVideoBean>> call, Response<List<CoachVideoBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<CoachVideoBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //大师问答列表
    public void getAnswer( String coach_id,final LoginListener loginListener){
        CoachService coachService = retrofit.create(CoachService.class);
        Call<List<AnswerBean>> call=coachService.getAnswer(coach_id);
        call.enqueue(new Callback<List<AnswerBean>>() {
            @Override
            public void onResponse(Call<List<AnswerBean>> call, Response<List<AnswerBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<AnswerBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //大师问答列表
    public void pushAsk( String user_id,String coach_id,String ask,final LoginListener loginListener){
        CoachService coachService = retrofit.create(CoachService.class);
        Call<SuccessBean> call=coachService.pushAsk(user_id,coach_id,ask);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

}
